package com.yjkf.zh.admin.controller;

import com.yjkf.zh.admin.aspect.LogAnnotation;
import com.yjkf.zh.admin.dao.SysDeptMapper;
import com.yjkf.zh.admin.model.SysDept;
import com.yjkf.zh.admin.security.SecurityUtils;
import com.yjkf.zh.admin.service.SysDeptService;
import com.yjkf.zh.core.http.HttpResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("dept")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysDeptMapper sysDeptMapper;


    @PreAuthorize("hasAuthority('sys:dept:view')")
    @GetMapping(value = "/findTree")
   // @LogAnnotation(content = "查看企业管理")
    public HttpResult findTree(HttpServletRequest request){
        Long userId = (Long)request.getSession().getAttribute("userId");
        return HttpResult.ok(sysDeptService.findTree(userId));
    }


    @PreAuthorize("hasAuthority('sys:dept:add') AND hasAuthority('sys:dept:edit')")
    @PostMapping(value = "/save")
    @LogAnnotation(content = "添加企业")
    public HttpResult save(@RequestBody SysDept sysDept){
        return HttpResult.ok(sysDeptService.save(sysDept));
    }

    @PreAuthorize("hasAuthority('sys:dept:delete')")
    @PostMapping(value = "/delete")
    @LogAnnotation(content = "删除企业")
    public HttpResult delete(@RequestBody List<SysDept> sysDept){
        return HttpResult.ok(sysDeptService.delete(sysDept));
    }

    @GetMapping(value = "/findTreeForId")
       public HttpResult findDeptForId(@RequestParam Long id,HttpServletRequest request){
        Long userId = (Long)request.getSession().getAttribute("userId");
        return HttpResult.ok(sysDeptMapper.selectDeptForId(userId,id));
    }

}
